package com.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//reduce 阶段
// k3  reduce阶段输入的key 类型Text
// v3  reduce阶段输入的value 类型 IntWritable
//k4  reduce阶段输出的key 类型 Text
//v4 reduce 阶段输出的value 类型IntWritable
public class WordCountReduce  extends Reducer<Text, IntWritable,Text,IntWritable> {
    private  IntWritable v4 = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //1. k4 = k3
        Text k4 = key;
        //2. v4 = v3元素的和：遍历v3求和
        int sum = 0;
        for (IntWritable value : values) {
         sum+=value.get();
        }
        v4.set(sum);
        //3.输出k4, v4：context.write()方法
        context.write(k4,v4);
    }
}
